clear all
set type double
version 18.5

cap program drop matrix4coefplot
program matrix4coefplot, rclass
	mat T = r(table)
	global i_last_columns=colnumb(T, "_cons") -1
	global i_first_columns =colnumb(T, "d1") -1
	mat B =  T[1,$i_first_columns..$i_last_columns]
	mat LL =  T[5,$i_first_columns..$i_last_columns]
	mat UL =  T[6,$i_first_columns..$i_last_columns]
	global colnames : colnames B
	global colnames = ustrregexra("$colnames", "^[0-9]+\.t0", "d0")
	di "$colnames"
	mat colname B = $colnames
	mat colname LL = $colnames
	mat colname UL = $colnames
	di "$d_lincom"
	lincom $d_lincom
	mat B[1,1] = -`r(estimate)'
	mat LL[1,1] = -`r(ub)'
	mat UL[1,1] = -`r(lb)'
	mat CI = (LL \ UL)
	mat rownames CI = ll95 ul95
	*mat list CI
	return matrix B = B, copy
	return matrix CI = CI, copy
end

use ../tmp/sample_090122,clear
do modules/__sample_for_timeseries.do

format MDYbid %td
***************************************************************************************
local days = 90
***************************************************************************************
local sample_day_first = "1apr2015"
local sample_day_last = "31mar2021"
local treatment_day_first = "15feb2019"
global n_months_a_year = floor(365 / `days')

keep if MDYbid <= td("`sample_day_last'") & MDYbid >= td("`sample_day_first'") 
cap drop time_to_treat
gen time_to_treat = floor((MDYbid - td("`treatment_day_first'"))/`days')
sort time_to_treat id
global t0_treatment = floor(365 * 4 / `days')
gen t0 = time_to_treat + $t0_treatment
qui sum t0
global t0_max = `r(max)'
gen treated = (firm_treatment == 1 & t0 >= $t0_treatment)
replace treated = . if firm_treatment == .

sum time_to_treat, d
global coeffs = ""
global d_lincom = "d1"

sum t0
forvalues x = 0(1)$t0_max{
	*if `x' !=$t0_treatment-1{
	gen d`x' = (firm_treatment == 1 & t0 == `x')
	if `x' < $t0_treatment{
		replace d`x' = 1 if firm_treatment == 1 & t0 == 0
		if `x' > 1{
			global d_lincom = "$d_lincom + d`x'"
		}
	}
	replace d`x' = . if firm_treatment == .

	if `x'> 0{
		global coeffs = "$coeffs d`x'"
	}
	label var d`x' "`x'"
	*}
}
di "t0_treatment = $t0_treatment"
global t0_xline = $t0_treatment
global coefplot_op = "legend(off) graphregion(c(white)) xlabel(, labsize(medium))"
global coefplot_op = "$coefplot_op ciopts(lcolor(gs7) ) mcolor(gs5) xline($t0_xline, lcolor(gs4)) yline(0, lc(gs4))"
global coefplot_op = `"$coefplot_op xlabel(1 "-16" 5 "-12" 9 "-8" 13 "-4" 17 "0" 21 "4" 25 "8")"'

xtset regionfirmID
* prob above reserve 
xtreg abovereserve i.t0 $coeffs if MDYbid >= td(01apr2015) & MDYbid < td(01apr2021) ///
	& winner != 1 & firm_treatment <. , fe cluster(g28_plus)
matrix4coefplot
mat B = r(B)
mat CI =r(CI)
coefplot matrix(B), ci(CI) vert $coefplot_op title("Pr(Losing Bids > Reserve)") 
graph export ../graph/event_twowaylpoly_abovereserve_timeseries.png, replace

* Raw Quaility
xtreg q i.t0 $coeffs if MDYbid >= td(01Mar2015) & MDYbid < td(01apr2021) & ///
	winner == 1 & firm_treatment <. , fe cluster(g28_plus)
matrix4coefplot
mat B = r(B)
mat CI =r(CI)
coefplot matrix(B), ci(CI) vert $coefplot_op title("Winning Bidders") 
graph export ../graph/event_twowaylpoly_q_winners_timeseries.png, replace

xtreg q i.t0 $coeffs if MDYbid >= td(01Mar2015) & MDYbid < td(01apr2021) & ///
	winner != 1 & firm_treatment <. , fe cluster(g28_plus)
matrix4coefplot
mat B = r(B)
mat CI =r(CI)
coefplot matrix(B), ci(CI) vert $coefplot_op title("Losing Bidders") 
graph export ../graph/event_twowaylpoly_q_losers_timeseries.png, replace

xtreg q i.t0 $coeffs if MDYbid >= td(01Mar2015) & MDYbid < td(01apr2021) & ///
	winner != 1 & firm_treatment <. & percent_reserve <= 1, fe cluster(g28_plus)
matrix4coefplot
mat B = r(B)
mat CI =r(CI)
coefplot matrix(B), ci(CI) vert $coefplot_op title("Losing Bidders") 
graph export ../graph/event_twowaylpoly_q_losers_below_reserve_timeseries.png, replace

* Normalized Bids
xtreg pctbid i.t0 $coeffs if MDYbid >= td(01Mar2015) & MDYbid < td(01apr2021) ///
	& winner == 1 & firm_treatment <., fe cluster(g28_plus)
matrix4coefplot
mat B = r(B)
mat CI =r(CI)
coefplot matrix(B), ci(CI) vert title("Winning Bidders") $coefplot_op 
graph export ../graph/event_twowaylpoly_winbidpct_timeseries.png, replace

xtreg pctbid i.t0 $coeffs if MDYbid >= td(01Mar2015) & MDYbid < td(01apr2021) ///
	& winner != 1 & firm_treatment <. , fe cluster(g28_plus)
matrix4coefplot
mat B = r(B)
mat CI =r(CI)
coefplot matrix(B), ci(CI) vert title("Losing Bidders") $coefplot_op 
graph export ../graph/event_twowaylpoly_pctlosingbids_timeseries.png, replace

